<?php
/**
 * @file:mpay_transaction_model.php
 * @author: hyman
 * @created on 2013-4-16
 * @description:mobile pay transaction model
 **/
class Mpay_transaction_model extends CI_Model {
    /**
     * ription: the default construct function.
     * 
     * @access public
     * @author hyman
     */
    public function __construct() {
        $this->sws = $this->load->database ( 'sws', TRUE );
    }
    
    /**
     * ription: get merchant order infos from databases by merchant id and order id.
     * 
     * @access public
     * @author hyman
     * @param int $merchant_id,
     *            int $order_id
     * @return mixed $order_info
     */
    public function get_merchant_order_info($merchant_id, $order_id) {
        $merchant_id = $this->sws->escape ( $merchant_id );
        $order_id = $this->sws->escape ( $order_id );
        
        $sql = "select 
					   TRAN_ID,
					   PAY_STATUS 
				from   sws_transtion 
				where  MERCHANT_ID=$merchant_id 
                     and ORDER_ID=$order_id limit 1";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->row_array ();
        }
    }
    
    /**
     * ription: insert transcation infos into database.
     * 
     * @access public
     * @author hyman
     * @param mixed $merchant_order_infos            
     * @return int $tran_id
     */
    public function insert_transcation($merchant_order_infos) {
        $data = array (
                "MERCHANT_ID" => $merchant_order_infos ['merchant_id'],
                "RETURN_URL" => $merchant_order_infos ['notice_url'],
                "BACK_URL" => $merchant_order_infos ['back_url'],
                "TRAN_TYPE" => $merchant_order_infos ['tran_type'],
                "PRODUCT_NAME" => $merchant_order_infos ['product_name'],
                "MONEY" => $merchant_order_infos ['money'],
                "ORDER_ID" => $merchant_order_infos ['order_id'],
                "PAY_STATUS" => $merchant_order_infos ['pay_status'],
                "RETURN_STATUS" => $merchant_order_infos ['return_status'],
                "CREATED_BY" => $merchant_order_infos ['created_by'],
                "CREATED_TIME" => $merchant_order_infos ['created_time'],
                "PAY_TYPE" => $merchant_order_infos ['pay_type'],
                "NOTE" => $merchant_order_infos ['note'] ,
                "USER_ID"=>$merchant_order_infos['user_id']
        );
        
        $this->sws->insert ( "SWS_TRANSACTION", $data );
        return $this->sws->insert_id ();
    }
    
    /**
     * ription: query transaction informations from database by $tran_id
     * 
     * @access public
     * @author hyman
     * @param int $tran_id            
     * @return mixed $transaction_info
     */
    public function get_transaction_info($tran_id) {
        $tran_id = $this->sws->escape ( $tran_id );
        $sql = "select " . "MONEY," . "PAY_STATUS," . "ORDER_ID," . "SWS_TRANSACTION.MERCHANT_ID," . "BACK_URL," . "RETURN_URL," . "MERCHANT_NAME," . "MERCHANT_KEY," . "PRODUCT_NAME," . "TRAN_TYPE," . "TRAN_CODE," . "PAYMENT_PAY_STATUS," . "PAYMENT_FEE," . "SWS_TRANSACTION.CREATED_TIME, " . "SERVER_NOTIFY_URL, " . "NOTE " . "from SWS_TRANSACTION " . "inner join SWS_MERCHANT on SWS_TRANSACTION.MERCHANT_ID = SWS_MERCHANT.MERCHANT_ID " . "where TRAN_ID =$tran_id limit 1";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->row_array ();
        }
    }
    
    /**
     * ription: update the order payer.
     * 
     * @access public
     * @author hyman
     * @param int $tran_id,int
     *            $account_no
     * @return null
     */
    public function update_order_payer($tran_id, $account_no) {
        $tran_id = $this->sws->escape ( $tran_id );
        $account_no = $this->sws->escape ( $account_no );
        $sql = "update SWS_TRANSACTION set ACCOUNT_NO =$account_no where TRAN_ID =$tran_id";
        $this->sws->query ( $sql );
    }
    
    /**
     * ription: get order infos by using order sn.
     * 
     * @access public
     * @author hyman
     * @param int $order_sn            
     * @return mixed $order_infos
     */
    public function query_order_by_sn($order_sn) {
        $order_sn = $this->sws->escape ( $order_sn );
        $sql = "select sro.TRAN_ID,sro.ACCOUNT_NO,sro.AMOUNT,tr.MERCHANT_ID from SWS_RECHARGE_ORDER as sro  left join SWS_TRANSACTION as tr on tr    .TRAN_ID = sro.TRAN_ID  where sro.ORDER_SN  = $order_sn";
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->row ();
        }
    }
    
    /**
     * ription: pay transaction.
     * 
     * @access public
     * @author hyman
     * @param mixed $transaction            
     * @param int $tran_id            
     * @return null
     */
    public function pay_transaction($transaction, $tran_id) {
        $data = array (
                "TRAN_CODE" => $transaction ['tran_code'],
                "PAY_STATUS" => $transaction ['pay_status'],
                "ACCOUNT_NO" => $transaction ['account_no'],
                "PAY_TIME" => $transaction ['pay_time'],
                "MODIFIED_TIME" => $transaction ['modified_time'],
                "MODIFIED_BY" => $transaction ['modified_by'],
                "PAYMENT_FEE" => $transaction ['payment_fee'] 
        );
        $this->sws->where ( "TRAN_ID", $tran_id );
        $this->sws->update ( "SWS_TRANSACTION", $data );
    }
    /**
     * ription:pay success..and change transaction return.
     * 
     * @access public
     * @author hyman
     * @param int $tran_id,int
     *            $return_status
     * @return null
     */
    public function change_transaction_retrun($tran_id, $return_status) {
        $tran_id = $this->sws->escape ( $tran_id );
        $return_status = $this->sws->escape ( $return_status );
        $sql = "update SWS_TRANSACTION set RETURN_STATUS=$return_status,RETURN_TIME =now() where TRAN_ID=$tran_id";
        $this->sws->query ( $sql );
    }
    public function update_new_order_info($tran_id, $money) {
        $money = $this->sws->escape ( $money );
        $tran_id = $this->sws->escape ( $tran_id );
        $sql = "update SWS_TRANSACTION set MONEY = $money where TRAN_ID = $tran_id";
        $this->sws->query ( $sql );
    }
    public function time_space_unreturn_order($time_low, $time_hight) {
        $sql = "select TRAN_ID,SWS_TRANSACTION.MERCHANT_ID,MONEY,MERCHANT_KEY,TRAN_CODE,ORDER_ID,RETURN_URL,SERVER_NOTIFY_URL,PAYMENT_FEE,PAYMENT_PAY_STATUS,NOTE from
			SWS_TRANSACTION inner join SWS_MERCHANT on SWS_TRANSACTION.MERCHANT_ID =SWS_MERCHANT.MERCHANT_ID
		 	where PAY_STATUS=1 
		 	and RETURN_STATUS=0 
		 	and timediff(now(),SWS_TRANSACTION.PAY_TIME)>='$time_low'
		 	and timediff(now(),SWS_TRANSACTION.PAY_TIME)<='$time_hight'";
        
        $query = $this->sws->query ( $sql );
        if ($query->num_rows () > 0) {
            return $query->result_array ();
        }
    }
}


/**
 *End of file 'mpay_transaction_model.php';
 *Location './mpay/models/mpay_transaction_model.php'
 */
